package com.yllt4cloud.ddauth.util.page;


import com.ffcs.icity.common.mybatis.Page;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;

import java.sql.Statement;
import java.util.List;
import java.util.Properties;

@Intercepts({@Signature(
        type = ResultSetHandler.class,
        method = "handleResultSets",
        args = {Statement.class}
)})
public class PaginationResultSetInterceptor implements Interceptor {
    public PaginationResultSetInterceptor() {
    }

    public Object intercept(Invocation invocation) throws Throwable {
        Object result = invocation.proceed();
        Page page = (Page)PaginationInterceptor.PAGE_CONTEXT.get();
        if (page != null) {
            if (result instanceof List) {
                page.setResult((List)result);
            }

            PaginationInterceptor.PAGE_CONTEXT.remove();
        }

        return result;
    }

    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    public void setProperties(Properties properties) {
    }
}

